<?
	class Ngo extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->helper('url');
			if($this->input->post('cancel')) 
				redirect('Ngo/read');
			$this->load->helper(array('form', 'url'));
			$this->load->library('form_validation');
			$this->load->library('session');
			$this->load->model('NgoModel');
			$this->load->model('UserModel');
			$this->load->model('DonationModel');
		}
		protected final function showView($fileName, $data)
		{
			$l = $this->load;
			$l->view('Templates/header.php', $data);
			$l->view('Ngos/' . $fileName . '.php', $data);
			$l->view('Templates/footer.php', $data);
		}
		protected final function getPostData()
		{
			$i = $this->input;
			$b = $this->NgoModel;
			$b->Name = $i->post('name');
			$b->Type = $i->post('type');
			$b->Description = $i->post('description');
			$b->Website = $i->post('webiste');
			$b->Landline = $i->post('landline');
			$b->Mobile = $i->post('mobile');
			$b->Email = $i->post('email');
			$b->Fax = $i->post('fax');
			$b->RegistrationNumber = $i->post('registrationNumber');
			return $b;
		}
		protected final function getIsPost()
		{
			return $_SERVER['REQUEST_METHOD'] == 'POST';
		}
		public final function create()
		{
			if($this->getIsPost())
			{
				$ngoId = $this->NgoModel->addNgo($this->getPostData());
				//
				$u = $this->UserModel;
				$u->FullName = 'admin';
				$u->Username = 'admin';
				$u->Password = md5('DefaultPassword');
				$u->Email = 'admin@gmail.com';
				$u->NgoId = $ngoId;
				$this->UserModel->addUser($u);
				//
				redirect('Ngo/read/' . $ngoId);
			}
			else
			{
				$data['title'] = 'Add an NGO';
				$this->showView('create', $data);
			}
		}
		public final function read($id = null)
		{
			if($id == null)
			{
				$data['title'] = 'View all NGOs';
				$data['ngos'] = $this->NgoModel->getAllNgos();
			}
			else
			{
				$data['title'] = 'View NGO details';
				$data['ngos'] = $this->NgoModel->getNgoById($id);
			}
			$this->showView('read', $data);
		}
		public final function update()
		{
			$data['title'] = 'Update an NGO';
			$data['ngo'] = $this->NgoModel->updateNgo($this->getPostData());
			$this->showView('update', $data);
		}
		public final function delete()
		{
			$id = $this->input->post('id');
			$data['title'] = 'Remove an NGO';
			$data['ngo'] = $this->NgoModel->removeNgoById($id);
			$this->showView('delete', $data);
		}
		public final function getDonationPostData()
		{
			$i = $this->input;
			$m = $this->DonationModel;
			$m->CompanyId = $i->post('companyId');
			$m->Amount = $i->post('Amount');
			$m->BeneficiaryId = $i->post('BeneficiaryId');
			$m->ExposeBenefactor = $i->post('ExposeBenefactor');
			return $m;
		}
		public final function acceptDonation()
		{
			if($this->getIsPost())
			{
				$this->NgoModel->acceptDonation($this->getDonationPostData());
			}
		}
		public final function login()
		{
			$data['title'] = 'NGO members login.';
			if($this->getIsPost())
			{
				$p = $this->input->post;
				$u = $this->UserModel->getUserByLoginCredential($p('username'), $p('password'));
				if($u !== NULL)
				{
					$this->session->set_userdata(array('user' => $u));
					redirect('Home/main');
				}
				else
				{
					$this->showView('login', $data);
				}
			}
			$this->showView('login', $data);
		}
		public final function logout()
		{
			$this->session->sess_destroy();
			redirect('Home/main');
		}
	}